package com.hdyg.sixg.activity.home;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.SmartWalletAdapter;
import com.hdyg.sixg.entry.SmartWalletBean;
import com.hdyg.sixg.mvp.Contrant.CHostWallet;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PHostWallet;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author CZB
 * @describe 托管钱包
 * @time 2019/4/9 11:07
 */
public class HostWalletActivity extends BaseActivity implements CHostWallet.IVHostWallet {
    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.ll_top_right)
    LinearLayout llTopRight;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private final static int REQUEST_CODE = 0x00001;
    private HeadViewHolder headViewHolder;
    private View headView;
    private SmartWalletAdapter mAdapter;
    private CHostWallet.IPHostWallet mPresenter;
    private int page = 1, allDataSize;
    private List<SmartWalletBean.ListBean> dataBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void initView() {
        onClickListener();
        headView = StringUtil.getView(R.layout.item_smart_wallet_top, mContext);
        headViewHolder = new HeadViewHolder(headView);
        tvTopTitle.setText(R.string.main_host_wallet);
        tvTopRight.setText(R.string.invers_trans);
        tvTopRight.setVisibility(View.VISIBLE);
        rvMain.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //转出
        llTopRight.setOnClickListener(v -> {
            startActivityForResult(HostTransActivity.class,REQUEST_CODE);
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.HOST_WALLET_URL, GetParamUtil.getSmartWalletParam(page));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PHostWallet(this);
    }

    private void initAdapter() {
        mAdapter = new SmartWalletAdapter(R.layout.item_smart_wallet, dataBeans);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.addHeaderView(headView);
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvMain);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_CODE:
                page = 1;
                initData();
                break;
        }
    }

    @Override
    public void vGetInitSuccess(SmartWalletBean dataBean) {
        dataBeans = dataBean.getList();
        allDataSize = dataBean.getTotal_num();
        headViewHolder.tvCurrent.setText(dataBean.getLive_wa());
        headViewHolder.tvDeposit.setText(dataBean.getFix_wa());
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(dataBeans);
            } else {
                initAdapter();
            }
        } else {
            if (dataBeans.size() != 0) {
                mAdapter.addData(dataBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }


    static class HeadViewHolder {

        @BindView(R.id.tv_deposit)
        TextView tvDeposit;
        @BindView(R.id.tv_current)
        TextView tvCurrent;

        HeadViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
